/**
 *
 */
package uno.utilities;

import java.io.FileReader;
import java.util.Scanner;

/**
 * @author lamberti
 *
 */
public class Configurator {

	public static final String SERVER_CONFIGFILE_PATH = "config/config.txt";
	public static final String CLIENT_CONFIGFILE_PATH = "config/client_config.txt";
	public static final boolean TYPE_CLIENT = true;
	public static final boolean TYPE_SERVER = false;
	private boolean type;
	private String [][]server_paramArray = {{"players", ""},{"waitingTime", ""},{"rmiPort", ""}};
	private String [][]client_paramArray = {{"serverPort",""},{"serverIP", ""},{"rmiPort", ""}};
	
	public Configurator(boolean type) { 	
		this.type = type;
		initialize();
	}

	/**
	 * Si legge tutte le righe del file di configurazione
	 * saltando i commenti ( # ) e aggiunge il valore del parametro alla matrice
	 * dei parametri. La posizione dei parametri nel file non e' rilevante.
	 * */
	private void initialize() {
		try{
			Scanner scan  = null;
			if(type == TYPE_SERVER) 
				scan = new Scanner(new FileReader(SERVER_CONFIGFILE_PATH));
			else if(type == TYPE_CLIENT)
				scan = new Scanner(new FileReader(CLIENT_CONFIGFILE_PATH));
			
			String currentLine;
			System.out.println("* parametri di configurazione:");
			while (scan.hasNextLine()) {
				currentLine = scan.nextLine();
				if(currentLine.length()> 0) {
					if(!currentLine.substring(0, 1).equals("#")) {
							Scanner scan2 = new Scanner(currentLine);
							scan2.useDelimiter("=");
							String currentParam = scan2.next().trim();
							
						if(scan2.hasNext()){
							String str = scan2.next().trim();
							addParamToList(type, currentParam, str);
						}
					}
				}
			}
		} catch(Exception e){
			e.printStackTrace();
		}
	}

	
	/**
	 * metodo del server
	 * */
	public String getMinPlayersNumber() {
		String minPlayersNum = null;
		if(type == TYPE_SERVER)
			minPlayersNum = server_paramArray[0][1];
		return minPlayersNum;
	}

	/**
	 * metodo del server
	 * */
	public String getWaitingTime() {
		String waitingTime = null;
		if(type == TYPE_SERVER)
			waitingTime = server_paramArray[1][1];
		return waitingTime;
	}

	/**
	 * metodo sia per il server che per il client
	 * */
	public String getRmiPort() {
		String rmiPort = null;
		if(type == TYPE_SERVER)
			rmiPort = server_paramArray[2][1];
		else if(type == TYPE_CLIENT)
			rmiPort = client_paramArray [2][1];
		return  rmiPort;
	}

	/**
	 * metodo del client
	 * */
	public String getServerPort() {
		String serverPort = null;
		if(type == TYPE_CLIENT)
			serverPort = client_paramArray [0][1];
		return serverPort;
	}
	
	/**
	 * metodo del client
	 * */
	public String getServerIp() {
		String serverIp = null;
		if(type == TYPE_CLIENT)
			serverIp = client_paramArray [1][1];
		return serverIp;
	}
	
	
	/**
	 * Scorre l'array dei parametri e controlla se il parametro passato va
	 * inserito, se si inserisce il parametro e il suo valore.
	 */
	private void addParamToList(boolean type,String parameter, String value) {
		String[][] paramArray = type ? client_paramArray : server_paramArray; 
		for(int k = 0; k < paramArray.length; k++){
			if(paramArray[k][0].equals(parameter)){
				paramArray[k][1] = value;
				System.out.println("*** " + parameter + ": " + value);
			}
		}
	}

}
